package com.atguigu.eud_teacher.service.impl;
import com.atguigu.commonutils.exceptionhandler.GuliException;
import com.atguigu.eud_teacher.dao.CourseDao;
import com.atguigu.eud_teacher.dao.EduTeacherDao;
import com.atguigu.eud_teacher.entity.CourseDescriptionEntity;
import com.atguigu.eud_teacher.entity.CourseEntity;
import com.atguigu.eud_teacher.entity.EduTeacherEntity;
import com.atguigu.eud_teacher.entity.vo.SubjectAndDescVo;
import com.atguigu.eud_teacher.service.ChapterService;
import com.atguigu.eud_teacher.service.CourseDescriptionService;
import com.atguigu.eud_teacher.service.CourseService;
import com.atguigu.eud_teacher.service.VideoService;
import com.atguigu.eud_teacher.vo.CourseInfoVo;
import com.atguigu.eud_teacher.vo.CoursePublishVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;
import sun.rmi.transport.ObjectTable;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("courseService")
public class CourseServiceImpl extends ServiceImpl<CourseDao, CourseEntity> implements CourseService {


    @Resource
    private CourseDescriptionService courseDescriptionService;

    @Resource
    private VideoService videoService;

    @Resource
    private ChapterService chapterService;

    @Resource
    private CourseDao courseDao;
    @Resource
    private EduTeacherDao eduTeacherDao;


    //添加课程基本信息，返回id给下一个页面
    @Override
    public String insertCourseAndDescription(CourseEntity courseEntity) {
        int flag = baseMapper.insert(courseEntity);
        String id;
        if (flag == 0) {
            throw new GuliException(20001, "插入课程基本信息错误");
        } else {
            CourseDescriptionEntity courseDescriptionInfo = new CourseDescriptionEntity();
            courseDescriptionInfo.setId(courseEntity.getId());
            courseDescriptionInfo.setDescription(courseEntity.getDescription());
            courseDescriptionService.save(courseDescriptionInfo);
            id = courseEntity.getId();
        }
        return id;
    }

    @Override
    //根据id查询基本信息，里面还有另外一张简介表。description
    public CourseEntity selectById(String id) {
        CourseEntity courseEntity = baseMapper.selectById(id);
        if (courseEntity == null) {
            throw new GuliException(20001, "没有这条数据，请输入正确的id值");
        }
        CourseDescriptionEntity courseDescriptionEntity = courseDescriptionService.getById(id);
        BeanUtils.copyProperties(courseDescriptionEntity, courseEntity);
        return courseEntity;
    }


    @Override
    //修改课程基本信息
    public void updateCourseInfo(CourseEntity courseEntity) {
        int flag = baseMapper.updateById(courseEntity);
        if (flag > 0) {
            CourseDescriptionEntity courseDescriptionEntity = new CourseDescriptionEntity();
            courseDescriptionEntity.setId(courseEntity.getId());
            courseDescriptionEntity.setDescription(courseEntity.getDescription());
            courseDescriptionService.updateById(courseDescriptionEntity);
        } else {
            throw new GuliException(20001, "修改课程基本信息失败，请核对");
        }
    }

    @Override
    public CoursePublishVo getFinalResult(String id) {
        return baseMapper.getFinalResult(id);
    }

    @Override
    public void deleteCourse(String courseId) {
        //先删除小节，章节，描述，课程本身体，视频用nacos删除
        videoService.removeVideoByCourseId(courseId);

        chapterService.removeChapterByCourseId(courseId);

        courseDescriptionService.removeCourseDescriptionByCourseId(courseId);

        int flag = baseMapper.deleteById(courseId);
        if (flag == 0) {
            throw new GuliException(20001, "删除课程失败");
        }
    }

    @Cacheable(value = "courseAndTeacher",key = "'selectIndexByHotSubjects'")
    @Override
    public Map<String, Object> selectIndexByHotSubjects() {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByDesc("id");
        wrapper.last("limit 8");
        List<CourseEntity> courseList = courseDao.selectList(wrapper);
        List<EduTeacherEntity> teacherList=eduTeacherDao.selectList(new QueryWrapper<EduTeacherEntity>()
                .orderByDesc("id").last("limit 4"));
        Map map=new HashMap();
        map.put("courseList",courseList);
        map.put("teacherList",teacherList);
        return map;
    }

    //分页加vo条件查询
    @Override
    public Map<String, Object> conditionSelect(long page, long limit, SubjectAndDescVo subjectAndDescVo) {
        Page<CourseEntity> pageList=new Page<>(page,limit);
        QueryWrapper<CourseEntity> queryWrapper=new QueryWrapper<>();
        String subjectParentId=subjectAndDescVo.getSubjectParentId();
        String subjectId = subjectAndDescVo.getSubjectId();
        String price=subjectAndDescVo.getPrice();
        String gmtCreate=subjectAndDescVo.getGmtCreate();
        String viewCount=subjectAndDescVo.getViewCount();
        if(!StringUtils.isEmpty(subjectParentId)){
            queryWrapper.eq("subject_parent_id",subjectAndDescVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(subjectId)){
            queryWrapper.eq("subject_id",subjectAndDescVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(viewCount)){
            queryWrapper.orderByDesc("view_count");
        }
        if(!StringUtils.isEmpty(price)){
            queryWrapper.orderByDesc("price");
        }
        if(!StringUtils.isEmpty(gmtCreate)){
            queryWrapper.orderByDesc("gmt_create");
        }
        IPage<CourseEntity> courseEntityIPage = courseDao.selectPage(pageList, queryWrapper);
        List<CourseEntity>  records=courseEntityIPage.getRecords();
        Long total=courseEntityIPage.getTotal();
        Map<String, Object> map=new HashMap<>();
        map.put("subjectId",subjectId);
        map.put("subjectParentId",subjectParentId);
        map.put("viewCount",viewCount);
        map.put("price",price);
        map.put("gmtCreate",gmtCreate);
        map.put("total",total);
        map.put("records",records);
        return map;
    }

    @Override
    public CourseInfoVo getBaseCourseInfo(String courseId) {
        CourseInfoVo courseInfoVo=courseDao.getBaseCourseInfo(courseId);
        return courseInfoVo;
    }

    @Override
    public com.atguigu.commonutils.ordervo.CourseInfoVo getBaseCourseInfoo(String courseId) {
        CourseInfoVo courseInfoVo=courseDao.getBaseCourseInfoo(courseId);
        com.atguigu.commonutils.ordervo.CourseInfoVo courseInfoVo1=new com.atguigu.commonutils.ordervo.CourseInfoVo();
        BeanUtils.copyProperties(courseInfoVo,courseInfoVo1);
        return courseInfoVo1;
    }
}